%this file is to calculate the 2-D correlation of u; it takes the 
%fluctuation fields as input

%Created by Ricardo Mejia-Alvarez.
%University of Illinois at Urbana-Champaign
%Urbana, IL. 06/16/2010

%modified:  06/22/2010

clear

%input data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
display('how many different data folders are you going to consider?');
NF = input('?  ');
folData = cell(NF);
pathFiles = cell(NF);
NOR = zeros(NF);

for k = 1 : NF
   
    kstr = num2str(NF);
    message = strcat('Enter folder number ',kstr,' \n');
    folData{k} = input( message , 's');
    folData{k} = strcat(folData{k},'\');
    
end


display('Now, for each data folder, tell me what columns to use to calculate')
display('two-point correlations (use vector format).')

for k = 1 : NF
   
    kstr = num2str(NF);
    message = strcat('Columns for data folder number ',kstr,' \n');
    DataColumns{k} = input( message ); %#ok<*SAGROW>
    cols(k) = length(DataColumns{k}); 
end

cols = sum(cols);
disp('What is the extension of the data files?')
disp('1. dat')
disp('2. V3D')
NK = input('?');

if NK == 1
    namekey = '*.dat';
else
    namekey = '*.V3D';
end

disp('Enter the spanwise location of streamwise traces.')
disp('Use format: [row-1 , row-2 , ... ]') 

SpanwiseLoc = input('?');

disp('Enter the streamwise location of spanmwise traces.')
disp('Use format: [column-1 , column-2 , ... ]') 

StreamwiseLoc = input('?');


disp('Enter data transformation if necessary in matrix form.')
disp('For instance: ')
disp('                    X  Y  U1  U2  U3  ...  Un')
disp('Inversion/scale   [-1  1  -1   1   1  ...   1 ]')
disp('Horizontal flip   [ 1  0   1   0   0  ...   0 ]')
disp('Vertical flip     [ 0  1   0   0   0  ...   0 ]')
disp('Transpose         [ 0  1   0   0   0  ...   0 ]')
disp('  ')
disp('Enter only the cells to modify.')
disp('E.g. T(1,1)=-1; T(1,4)=-1; T(2,1)=1; T(2,4)=1; T(3,2)=1; T(4,2)=1')
disp('   ')
disp('Type "return" and press the Return key to finish')
T = ones(1 , cols + 2);
T(2 : 4 , :) = zeros(3 , cols + 2);

keyboard

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%creating the directory structure of the analysis
[~,folResults,folFluc] = newfolder(folData{1},'results','fluctuations'); 

for k = 1 : NF

    %generating a vector with the name of the files to process
    pathFiles{k} = filesVector(folData{k},namekey);

    %finding the number of instantaneous realizations
    NOR(k) = length(pathFiles{k});

end


testLength = isequal(NOR , repmat(NOR(1) , [1 , length(NOR)] ) );
if testLength == 0
    display('The number of files in the different folders is not the same.')
    display('I think you should take a look at this.')
    display('hit Return to continue')
    pause
    testLength = [];
end

tic     %measures total time
tic     %measures calculation + data loading time

NOR = NOR(1);

for n = 1 : NOR

    p = 0;
    for k = 1 : NF
        filename = char(pathFiles{k}(n));
        [~,I,J,Dx,Dy,A] = matrix(filename);

        for m = 1 : length( DataColumns{k} )

            U(p + m) = { A{ DataColumns{k}(m) } };

        end
        p = length(U);

        [U] = Transformation(T(: , 3 : end) , U);

        if k == 1 && n == 1
            [A(1 : 2)] = Transformation(T(: , 1 : 2) , A(1 : 2));

            X = A{1};
            Y = A{2};
            
            rows = 1 : J;
            cols = 1 : I;
            
            Location_RC = [ones(length(cols) , 1) * SpanwiseLoc(1) , cols'];
 
            for jj = 2 : length(SpanwiseLoc)
                Location_RC = cat( 1 , Location_RC , [ones(length(cols) , 1) * SpanwiseLoc(jj) , cols'] );
            end
            
%             for jj = 1 : length(StreamwiseLoc)
%                 Location_RC = cat( 1 , Location_RC , [rows' , ones(length(rows) , 1) * StreamwiseLoc(jj)] );
%             end
%             
            LRC = length( Location_RC(:,1) );

        end

    end

    for mm = 1 : LRC
        for nn = 1 : p
            V(mm , nn) = U{nn}( Location_RC(mm,1) , Location_RC(mm,2) );
        end
    end

    [Uc] = cross_variables(U , V);


    if n ~= 1

        for kk = 1 : length(Uc)
            Ucorr{kk} = Ucorr{kk} + Uc{kk};
        end

    else

        Ucorr = Uc;

    end

end

for kk = 1 : length(Uc)
    Ucorr{kk} = Ucorr{kk} / NOR;
end

toc

tic     %measures saving-time

disp('saving results')
tic

ti = num2str(I);
tj = num2str(J);


filename = strcat('\Non-Homog_Two-point-correlation.dat');

for k = 1 : LRC
    zoneX = num2str(Location_RC(k , 1));
    zoneY = num2str(Location_RC(k , 2));

    heading = strcat('ZONE T = "[',zoneX,',' , zoneY ,']" I = ' , ti , ' J = ', tj , ' F = POINT');
    
    data = cat( 3 , DeltaX(: , : , k) , Y(: , : , k) , Ucorr{k} );
    
    data = mixingMat(I , J , data);
    data = sortrows(data , [2 , 1]);

    if k ~= 1
        saverappend(folResults,filename,heading,data)
    else
        saver(folResults,filename,heading,data)
    end

end
toc
    
   
display('Total calculation time')
toc

